import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import edu.jhu.tcj.purfuit.Category;
import edu.jhu.tcj.purfuit.CategoryManager;
import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.Player;


public class PlayerSetupPanel extends GamePanel {
	private JTextField player1TextBox;
	private Color clrPlayer1 = Color.red;
	private JTextField player2TextBox;
	private Color clrPlayer2 = Color.white;
	private JTextField player3TextBox;
	private Color clrPlayer3 = Color.blue;
	private JTextField player4TextBox;
	private Color clrPlayer4 = Color.green;
	private Application application;
	private SoundManager soundManager;
	public PlayerSetupPanel(final Application application, SoundManager sm) {
		soundManager = sm;
		setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		setLayout(null);
		
		Font nameFont = new Font("Tahoma", Font.PLAIN, 30);

		this.application = application;
		
		player1TextBox = new JTextField();
		player1TextBox.setText("Player 1");
		player1TextBox.setFont(nameFont);
		player1TextBox.setBounds(406, 55, 328, 61);
		add(player1TextBox);
		player1TextBox.setColumns(10);
		

		
		JLabel lblNewLabel = new JLabel("Player 1's Name:");
		lblNewLabel.setForeground(Color.LIGHT_GRAY);
		lblNewLabel.setFont(nameFont);
		lblNewLabel.setBounds(137, 55, 231, 61);
		add(lblNewLabel);
		
		player2TextBox = new JTextField();
		player2TextBox.setText("Player 2");
		player2TextBox.setFont(nameFont);
		player2TextBox.setColumns(10);
		player2TextBox.setBounds(406, 156, 328, 61);
		add(player2TextBox);
		
		player3TextBox = new JTextField();
		player3TextBox.setText("Player 3");
		player3TextBox.setFont(nameFont);
		player3TextBox.setColumns(10);
		player3TextBox.setBounds(406, 257, 328, 61);
		add(player3TextBox);
		
		player4TextBox = new JTextField();
		player4TextBox.setText("Player 4");
		player4TextBox.setFont(nameFont);
		player4TextBox.setColumns(10);
		player4TextBox.setBounds(406, 356, 328, 61);
		add(player4TextBox);
		
		JLabel lblPlayersName = new JLabel("Player 2's Name:");
		lblPlayersName.setForeground(Color.LIGHT_GRAY);
		lblPlayersName.setFont(nameFont);
		lblPlayersName.setBounds(137, 156, 231, 61);
		add(lblPlayersName);
		
		JLabel lblPlayersName_2 = new JLabel("Player 4's Name:");
		lblPlayersName_2.setForeground(Color.LIGHT_GRAY);
		lblPlayersName_2.setFont(nameFont);
		lblPlayersName_2.setBounds(137, 356, 231, 61);
		add(lblPlayersName_2);
		
		JLabel lblPlayersName_1 = new JLabel("Player 3's Name:");
		lblPlayersName_1.setForeground(Color.LIGHT_GRAY);
		lblPlayersName_1.setFont(nameFont);
		lblPlayersName_1.setBounds(137, 257, 231, 61);
		add(lblPlayersName_1);
	}
	
	public void mouseClicked(MouseEvent e) {
		int x = e.getPoint().x;
		int y = e.getPoint().y-50;
		System.out.println(x + " " + y);

		if(x< 565 && x > 284 && y < 513 && y > 447)
		{
			if (player1TextBox.getText() == null || player1TextBox.getText().length() == 0) {
				return;
			}
			Player player1 = new Player(player1TextBox.getText(), clrPlayer1);
			
			Player[] players;
			if (player2TextBox.getText() == null || player2TextBox.getText().length() == 0) {
				players = new Player[]{player1};
			} else {
				Player player2 = new Player(player2TextBox.getText(), clrPlayer2);
				if (player3TextBox.getText() == null || player3TextBox.getText().length() == 0) {
					players = new Player[]{player1, player2};
				} else {
					Player player3 = new Player(player3TextBox.getText(), clrPlayer3);
					if (player4TextBox.getText() == null || player4TextBox.getText().length() == 0) {
						players = new Player[]{player1, player2, player3};
					} else {
						Player player4 = new Player(player4TextBox.getText(), clrPlayer4);
						players = new Player[]{player1, player2, player3, player4};
					}
				}
			}
			Category[] categories = CategoryManager.getInstance().getCategories();
			Game game = new Game(players, categories);
			soundManager.playSelectNoise();
			application.showGameBoardPanel(game);

		}
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(new Color(79	, 129, 189));
		g.fillOval(0, 0, 80, 80);
		g.fillOval(716, 0, 80, 80);
		g.fillOval(0, 470, 80, 80);
		g.fillOval(716, 470, 80, 80);
		g.fillRect(40,0,716,600);
		g.fillRect(0,40,800,470);
		g.setColor(clrPlayer1);
		g.fillRoundRect(60, 60, 45, 45,15,15);
		g.setColor(clrPlayer2);
		g.fillRoundRect(60, 160, 45, 45,15,15);
		g.setColor(clrPlayer3);
		g.fillRoundRect(60, 260, 45, 45,15,15);
		g.setColor(clrPlayer4);
		g.fillRoundRect(60, 360, 45, 45,15,15);

		g.setColor(new Color(183,178,178));
		g.fillRect(284, 447, 280, 66);
		
		g.setColor(Color.black);
		g.setFont(new Font("Tahoma", Font.PLAIN, 28));
		g.drawString("Click to Start", 350, 492);

	}
}
